<?php

global $_GPC;
global $_W;
$cfg = $this->module['config'];
if ($_W['openid']) {
    header('location:' . $this->createMobileUrl('phone'));
    exit(0);
}
session_start();
$userid = $_GPC['_userid'];
if (!$userid) {
    header('location:' . $this->createMobileUrl('login'));
    exit(0);
}
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$user = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE id =:a', array(':a' => $userid));
if (!$user || !$user['status']) {
    message('请添加门店和员工信息！', '', 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE id =:a', array(':a' => $user['pcate']));
$shopid = $shop['id'];
load()->model('mc');
if (!pdo_fieldexists('j_hmoney_user', 'isdiscount')) {
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_user') . ' ADD `isdiscount` tinyint(1) NOT NULL DEFAULT \'0\' ;');
}
if (!pdo_fieldexists('j_hmoney_refund', 'goodsn')) {
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_refund') . ' ADD `goodsn` varchar(20) NOT NULL COMMENT \'\';');
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_refund') . ' ADD `title` varchar(50) NOT NULL COMMENT \'\';');
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_refund') . ' ADD `price` decimal(10,2) NOT NULL DEFAULT \'0\' COMMENT \'\'');
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_refund') . ' ADD `ispay` tinyint(1) NOT NULL DEFAULT \'0\' COMMENT \'\';');
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_refund') . ' ADD `kprinttime` tinyint(2) NOT NULL DEFAULT \'0\' COMMENT \'\';');
    pdo_query('ALTER TABLE ' . tablename('j_hmoney_refund') . ' ADD `reason` varchar(50) NOT NULL COMMENT \'理由\';');
}
if ($operation == 'display') {
    $tablepcatelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by displayorder asc,id desc');
    $categoryary = array();
    foreach ($tablepcatelist as $row) {
        $categoryary[$row['id']] = $row['title'];
    }
    $goodpcatelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1  order by  displayorder asc, id desc');
    $isNoPcate = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and pcate=0');
    $isfungood = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_goodmarket') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1 and starttime<=\'' . TIMESTAMP . '\' and endtime>=\'' . TIMESTAMP . '\' and contentid<>\'\' ');
    $ispackage = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_goodpackage') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1 ');
    $printerlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_cookprint') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and ptype=0 order by id desc');
} else {
    if ($operation == 'logout') {
        isetcookie('_userid', '', -1);
        isetcookie('siteuniacid', '', -1);
        isetcookie('siteroot', '', -1);
        message('退出成功', $this->createMobileUrl('login'), 'success');
    } else {
        if ($operation == 'changepassword') {
            $opass = md5($_GPC['opass']);
            $newpass = md5($_GPC['newpass']);
            if ($user['password'] != $opass) {
                exit(json_encode(array('success' => false, 'msg' => '原密码错误')));
            }
            pdo_update('j_hmoney_user', array('password' => $newpass), array('id' => $user['id']));
            exit(json_encode(array('success' => true)));
        } else {
            if ($operation == 'getpcatetable') {
                $pcate = intval($_GPC['pcate']);
                if (!$pcate) {
                    $pcate = pdo_fetchcolumn('SELECT id FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by displayorder asc,id desc limit 1');
                }
                $tablelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=:a order by newctrl desc,status desc ,displayorder asc,id asc', array(':a' => $pcate));
                $num0 = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=0');
                $num1 = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1');
                $num2 = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=2');
                $allnum = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' ');
                exit(json_encode(array('success' => true, 'list' => $tablelist, 'num0' => $num0, 'num1' => $num1, 'num2' => $num2, 'allnum' => $allnum)));
            } else {
                if ($operation == 'getpcategood') {
                    $pcate = intval($_GPC['pcate']);
                    $goodslist = array();
                    if ($pcate == -1) {
                        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodmarket') . ' WHERE groupid=:a and status=1 and starttime<=:b and endtime>=:b and contentid<>\'\' order by id desc', array(':a' => $shopid, ':b' => TIMESTAMP));
                        foreach ($list as $row) {
                            if ($row['limitweek']) {
                                $day = date('w') ? date('w') : 7;
                                if (!in_array($day, explode(',', $row['limitweek']))) {
                                    continue;
                                } else {
                                    if ($row['limithour']) {
                                        $time = intval(date('h'));
                                        if (!in_array($time, explode(',', $row['limithour']))) {
                                            continue;
                                        } else {
                                            $goods = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE id in(' . $row['contentid'] . ') and status=1 and store<>0 order by id desc');
                                            $i = 0;
                                            while ($i < count($goods)) {
                                                $temp = $goods[$i];
                                                if (strpos($row['discount'], '%')) {
                                                    $temp['title'] = '[' . str_replace('%', '', $row['discount']) / 10 . '折]' . $goods[$i]['title'];
                                                    str_replace('%', '', $row['discount']);
                                                    $temp['marketprice'] = $this->_2money($goods[$i]['marketprice'] * str_replace('%', '', $row['discount']));
                                                }
                                                if (strpos($row['discount'], '-') !== false) {
                                                    abs($row['discount']);
                                                    if ($goods[$i]['marketprice'] - abs($row['discount']) >= 0) {
                                                        $temp['title'] = '[' . str_replace('-', '立减', $row['discount']) . ']' . $goods[$i]['title'];
                                                        abs($row['discount']);
                                                        $temp['marketprice'] = $this->_2money(($goods[$i]['marketprice'] - abs($row['discount'])) * 100);
                                                    }
                                                }
                                                array_push($goodslist, $temp);
                                                $i = $i + 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if ($pcate == -2) {
                            $goodslist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodpackage') . ' WHERE groupid=:a and status=1 order by displayorder asc,id asc', array(':a' => $shopid));
                        } else {
                            $goodslist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and pcate=:a and groupid=:b and status=1 and store<>0 order by displayorder asc,id asc', array(':a' => $pcate, ':b' => $shopid));
                        }
                    }
                    $i = 0;
                    while ($i < count($goodslist)) {
                        if ($goodslist[$i]['thumb']) {
                            $goodslist[$i]['thumb'] = tomedia($goodslist[$i]['thumb']);
                        }
                        $i = $i + 1;
                    }
                    exit(json_encode(array('success' => true, 'list' => $goodslist, 'a' => count($goods))));
                } else {
                    if ($operation == 'getgoodpackage') {
                        $id = intval($_GPC['id']);
                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goodpackage') . ' WHERE id=:a', array(':a' => $id));
                        $goodtemp = explode(',', $item['goodids']);
                        $gids = array();
                        $gprice = array();
                        foreach ($goodtemp as $row) {
                            $temp = explode(':', $row);
                            $gids[] = $temp[0];
                            $gprice[] = $temp[1];
                        }
                        $good1 = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id in (' . implode(',', $gids) . ') order by id desc  ');
                        $goodtemp1 = array();
                        foreach ($good1 as $row) {
                            $goodtemp1[$row['id']] = $row;
                        }
                        $goodary = array();
                        foreach ($gids as $index => $row) {
                            $goodary[$index] = $goodtemp1[$row];
                            if ($goodtemp1[$row]['thumb']) {
                                $goodary[$index]['thumb'] = tomedia($goodtemp1[$row]['thumb']);
                            }
                            $goodary[$index]['price'] = $gprice[$index];
                        }
                        exit(json_encode(array('success' => true, 'list' => $goodary, 'a' => count($goodary))));
                    } else {
                        if ($operation == 'selectgood') {
                            $id = intval($_GPC['id']);
                            $num = intval($_GPC['num']);
                            $mid = intval($_GPC['mid']);
                            if (!$num || !$num) {
                                exit(json_encode(array('success' => false, 0 => '异常错误')));
                            }
                            $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=:a and id=:b', array(':a' => $shopid, ':b' => $id));
                            if (!$goods) {
                                exit(json_encode(array('success' => false, 0 => '无此商品')));
                            }
                            if ($mid) {
                                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE id=:b', array(':b' => $id));
                                $hnum = $item['num'];
                                if ($num == $hnum) {
                                    exit(json_encode(array('success' => true)));
                                } else {
                                    if ($num < $hnum) {
                                        exit(json_encode(array('success' => true)));
                                    } else {
                                        if ($num > $hnum) {
                                            if ($num - $hnum > $goods['store']) {
                                                exit(json_encode(array('success' => false, 'msg' => '库存不足，差【' . ($num - $hnum - $goods['store']) . '】个')));
                                            } else {
                                                if ($num - $hnum <= $goods['store']) {
                                                    exit(json_encode(array('success' => true)));
                                                }
                                            }
                                        }
                                    }
                                }
                            } else {
                                if (!$goods['status']) {
                                    exit(json_encode(array('success' => false, 'msg' => '该商品已经下架')));
                                }
                                if ($goods['store'] == 0) {
                                    exit(json_encode(array('success' => false, 'msg' => '库存不足')));
                                }
                                if ($goods['store'] < 0) {
                                    exit(json_encode(array('success' => true)));
                                }
                                if ($num > $goods['store']) {
                                    exit(json_encode(array('success' => false, 'msg' => '库存不足，差【' . $num - $goods['store'] . '】个')));
                                }
                                exit(json_encode(array('success' => true)));
                            }
                        } else {
                            if ($operation == 'searchgood') {
                                $keyword = trim($_GPC['keyword']);
                                if (!$keyword) {
                                    exit(json_encode(array('success' => false, 'goods' => '无此商品')));
                                }
                                $sql = ' and goodsn=\'' . $keyword . '\' ';
                                $pattern = '/^[a-zA-Z\\s]+$/';
                                if (preg_match($pattern, strtolower($keyword))) {
                                    $sql = ' and shorttitle=\'' . $keyword . '\' ';
                                }
                                $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:a ' . $sql . ' order by displayorder asc,id asc limit 1', array(':a' => $shopid));
                                if (!$goods) {
                                    if (strlen($keyword) < 6 || strpos($keyword, '.')) {
                                        $goods = array('marketprice' => $this->_2money($keyword * 100), 'memberprice' => 0, 'title' => '', 'goodsn' => '', 'id' => 0);
                                    }
                                }
                                exit(json_encode(array('success' => true, 'goods' => $goods)));
                            } else {
                                if ($operation == 'fastgood') {
                                    $keyword = trim($_GPC['keyword']);
                                    if (!$keyword) {
                                        exit(json_encode(array('success' => false, 'msg' => '无此商品')));
                                    }
                                    $sql = ' and goodsn like \'%' . $keyword . '%\' ';
                                    $pattern = '/^[a-zA-Z\\s]+$/';
                                    if (preg_match($pattern, strtolower($keyword))) {
                                        $sql = ' and shorttitle like \'%' . $keyword . '%\' ';
                                    }
                                    $goods = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=' . $shopid . ' ' . $sql . ' and status=1 and store<>0 order by id desc ');
                                    if ($goods) {
                                        $i = 0;
                                        while ($i < count($goods)) {
                                            $goods[$i]['thumb'] = tomedia($goods[$i]['thumb']);
                                            $i = $i + 1;
                                        }
                                    }
                                    exit(json_encode(array('success' => true, 'goods' => $goods)));
                                } else {
                                    if ($operation == 'postneworder') {
                                        $tid = $_GPC['tid'];
                                        $num = $_GPC['num'];
                                        $orderid = $_GPC['orderid'];
                                        $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $tid));
                                        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                        if ($table['status'] == 0) {
                                            $hasOldorder = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and tableid=:a and ispay=0 ', array(':a' => $table['id']));
                                            if ($hasOldorder) {
                                                exit(json_encode(array('success' => false, 'msg' => '该餐桌还有订单未处理，请先处理该订单')));
                                            }
                                            $orderDisplayorder = pdo_fetchcolumn('SELECT serialnum  FROM ' . tablename('j_hmoney_order') . ' WHERE weid=:c and groupid=:a and createdate=:b order by id desc limit 1', array(':a' => $shopid, ':b' => date('Y-m-d'), ':c' => $_W['uniacid']));
                                            $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'userid' => $userid, 'serialnum' => $orderDisplayorder + 1, 'out_trade_no' => TIMESTAMP . mt_rand(100, 999), 'tableid' => $tid, 'person' => $num, 'teafee' => intval(($num * $table['perperson'] + $table['teafee']) * 100), 'createtime' => TIMESTAMP, 'createdate' => date('Y-m-d'));
                                            pdo_insert('j_hmoney_order', $data);
                                            $orderid = pdo_insertid();
                                            pdo_update('j_hmoney_table', array('status' => 1, 'orderid' => $orderid, 'createtime' => TIMESTAMP), array('id' => $tid));
                                            $this->wildDog_socket($shopid);
                                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                            exit(json_encode(array('success' => true, 'table' => $table, 'trade' => $trade)));
                                        } else {
                                            if ($table['status'] == 1) {
                                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and tableid=:a order by id desc limit 1', array(':a' => $tid));
                                                if ($orderid) {
                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                }
                                                $goodlist = pdo_fetchall('SELECT a.*,b.memberprice,b.unitname,b.marketprice FROM ' . tablename('j_hmoney_ordergoods') . ' as a LEFT JOIN ' . tablename('j_hmoney_goods') . ' as b ON a.goodid=b.id  WHERE a.out_trade_no=:a order by a.id desc ', array(':a' => $trade['out_trade_no']));
                                                exit(json_encode(array('success' => true, 'table' => $table, 'trade' => $trade, 'goods' => $goodlist)));
                                            } else {
                                                if ($table['status'] == 2) {
                                                    $tradelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and tableid=:a and status<2 order by id desc  ', array(':a' => $tid));
                                                    if (count($tradelist) == 1) {
                                                        $trade = $tradelist[0];
                                                        $goodlist = pdo_fetchall('SELECT a.*,b.memberprice,b.unitname,b.marketprice FROM ' . tablename('j_hmoney_ordergoods') . ' as a LEFT JOIN ' . tablename('j_hmoney_goods') . ' as b ON a.goodid=b.id  WHERE a.out_trade_no=:a order by a.id desc ', array(':a' => $trade['out_trade_no']));
                                                        exit(json_encode(array('success' => true, 'table' => $table, 'trade' => $trade, 'goods' => $goodlist)));
                                                    } else {
                                                        exit(json_encode(array('success' => true, 'table' => $table, 'tradelist' => $tradelist)));
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        if ($operation == 'submitorder') {
                                            $orderid = $_GPC['orderid'];
                                            $param = $_GPC['param'];
                                            if (!$orderid) {
                                                exit(json_encode(array('success' => false, 'msg' => '订单编号不能为空')));
                                            }
                                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                            $sqlinsert = 'insert into ' . tablename('j_hmoney_ordergoods') . ' (weid,groupid,orderid,createtime,out_trade_no,goodid,goodsn,title,price,oldprice,num,total_fee,status,ispack,taste)VALUES';
                                            $tempInsertAry = array();
                                            $tempUpdateAry = array();
                                            $goodisAry = array();
                                            $goodNewisAry = array();
                                            $MidAry = array();
                                            $MidNewAry = array();
                                            $itemlist = pdo_fetchall('SELECT id,goodid,num FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid = :a ', array(':a' => $orderid));
                                            foreach ($itemlist as $row) {
                                                $MidAry[$row['id']] = $row['num'];
                                                if (!isset($goodisAry[$row['goodid']])) {
                                                    $goodisAry[$row['goodid']] = 0;
                                                }
                                                $goodisAry[$row['goodid']] = $goodisAry[$row['goodid']] + $row['num'];
                                            }
                                            $isnewgood = false;
                                            foreach ($param as $row) {
                                                $tempAry = explode('|', $row);
                                                $num += $tempAry[7];
                                                if ($goodNewisAry[$tempAry[1]] == NULL) {
                                                    $goodNewisAry[$tempAry[1]] = $tempAry[7];
                                                } else {
                                                    $goodNewisAry[$tempAry[1]] = $tempAry[7] + $goodNewisAry[$tempAry[1]];
                                                }
                                                if ($tempAry[0] == 'undefined') {
                                                    $isnewgood = true;
                                                    intval($tempAry[8] * 100);
                                                    intval($tempAry[9]);
                                                    $tempInsertAry[] = '(\'' . implode('\',\'', array(0 => $_W['uniacid'], 1 => $shopid, 2 => $orderid, 3 => TIMESTAMP, 4 => $trade['out_trade_no'], 5 => $tempAry[1], 6 => $tempAry[2], 7 => $tempAry[3], 8 => $tempAry[4], 9 => $tempAry[6], 10 => $tempAry[7], 11 => intval($tempAry[8] * 100), 12 => 1, 13 => intval($tempAry[9]), 14 => $tempAry[10])) . '\')';
                                                } else {
                                                    $MidNewAry[$tempAry[0]] = $tempAry[7];
                                                    $tempUpdateAry[$tempAry[0]] = array('price' => $tempAry[4], 'num' => $tempAry[7], 'fee' => intval($tempAry[8] * 100), 'ispack' => intval($tempAry[9]), 'taste' => $tempAry[10]);
                                                }
                                            }
                                            unset($goodisArya);
                                            if (count($goodNewisAry)) {
                                                $goodataAry = array();
                                                $goodlist = pdo_fetchall('SELECT id,store,title FROM ' . tablename('j_hmoney_goods') . ' WHERE id in (' . implode(',', array_keys($goodNewisAry)) . ') and store>-1 ');
                                                foreach ($goodlist as $row) {
                                                    if ($row['store'] - ($goodNewisAry[$row['id']] - $goodisAry[$row['id']]) < 0) {
                                                        exit(json_encode(array('success' => false, 'msg' => '【' . $row['title'] . '】库存不足,差【' . abs($row['store'] - $goodisAry[$row['id']]) . '】')));
                                                    } else {
                                                        $goodataAry[$row['id']] = $row['store'] - ($goodNewisAry[$row['id']] - $goodisAry[$row['id']]);
                                                        continue;
                                                    }
                                                }
                                            }
                                            pdo_update('j_hmoney_table', array('status' => 2, 'endtime' => TIMESTAMP), array('id' => $trade['tableid']));
                                            $sql = count($tempInsertAry) ? $sqlinsert . implode(',', $tempInsertAry) . ';' : '';
                                            pdo_run($sql);
                                            foreach ($MidAry as $index => $row) {
                                                if (isset($MidNewAry[$index])) {
                                                    unset($MidAry[$index]);
                                                }
                                            }
                                            if (count($MidAry)) {
                                                pdo_delete('j_hmoney_ordergoods', ' id in (' . implode(',', array_keys($MidAry)) . ')');
                                            }
                                            if (count($tempUpdateAry)) {
                                                foreach ($tempUpdateAry as $index => $row) {
                                                    pdo_update('j_hmoney_ordergoods', array('price' => $row['price'], 'num' => $row['num'], 'total_fee' => $row['fee'], 'ispack' => $row['ispack'], 'taste' => $row['taste']), array('id' => $index));
                                                }
                                            }
                                            if (count($goodataAry)) {
                                                foreach ($goodataAry as $index => $row) {
                                                    pdo_update('j_hmoney_goods', array('store' => $row), array('id' => $index));
                                                }
                                            }
                                            $gather = pdo_fetch('SELECT sum(num) as num1,count(*) as num2,sum(total_fee) as tfee,sum(oldprice*num*100) as ofee FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and out_trade_no=:a ', array(':a' => $trade['out_trade_no']));
                                            $gather2 = pdo_fetch('SELECT sum(a.total_fee) as notfee FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE a.weid = \'' . $_W['uniacid'] . '\' and b.isnotcoupon=1 and a.out_trade_no=:a ', array(':a' => $trade['out_trade_no']));
                                            $data = array('order_fee' => $gather['tfee'], 'total_fee' => $gather['tfee'], 'notdiscount_fee' => $gather2['notfee'], 'num' => $gather['num1'], 'species' => $gather['num2'], 'status' => 1);
                                            pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                            if ($trade['status'] == 1) {
                                                $isnewgood = true;
                                            } else {
                                                $isnewgood = false;
                                            }
                                            $this->wildDog_socket($shopid);
                                            exit(json_encode(array('success' => true, 'isnewgood' => $isnewgood)));
                                        } else {
                                            if ($operation == 'cleartable') {
                                                $tid = $_GPC['tid'];
                                                $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $tid));
                                                if ($table['status'] != 1) {
                                                    exit(json_encode(array('success' => false, 'msg' => '还有订单没有处理，不能清台。')));
                                                }
                                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and tableid=:a and status=0 and ispay=0 order by id desc limit 1', array(':a' => $tid));
                                                if (!$trade || !$trade['status']) {
                                                    pdo_update('j_hmoney_table', array('status' => 0, 'orderid' => 0, 'pauseorder' => 0, 'ispay' => 0, 'ismore' => 0, 'createtime' => 0, 'endtime' => 0, 'newctrl' => 0), array('id' => $tid));
                                                    pdo_delete('j_hmoney_ordergoods', array('orderid' => $trade['id']));
                                                    pdo_delete('j_hmoney_order', array('id' => $trade['id']));
                                                    $this->wildDog_socket($shopid);
                                                    exit(json_encode(array('success' => true)));
                                                }
                                                exit(json_encode(array('success' => false, 'msg' => '还有订单没有处理，不能清台。')));
                                            } else {
                                                if ($operation == 'finishorder') {
                                                    $tid = $_GPC['tid'];
                                                    $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid= :b and id=:a ', array(':a' => $tid, ':b' => $_W['uniacid']));
                                                    if ($table['status'] != 2) {
                                                        exit(json_encode(array('success' => false, 'msg' => '还有订单没有处理!')));
                                                    }
                                                    $tradelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid=:b and tableid=:a and status<2 and status>-1 order by id desc ', array(':a' => $tid, ':b' => $_W['uniacid']));
                                                    foreach ($tradelist as $row) {
                                                        if ($row['ispay'] != 1) {
                                                            exit(json_encode(array('success' => false, 'msg' => '还有订单没有处理')));
                                                        }
                                                    }
                                                    foreach ($tradelist as $row) {
                                                        pdo_update('j_hmoney_order', array('status' => 2), array('id' => $row['id']));
                                                        pdo_update('j_hmoney_ordergoods', array('status' => 2), array('orderid' => $row['id']));
                                                    }
                                                    pdo_update('j_hmoney_table', array('status' => 0, 'orderid' => 0, 'pauseorder' => 0, 'ispay' => 0, 'newctrl' => 0, 'ismore' => 0, 'createtime' => 0, 'endtime' => 0), array('id' => $tid));
                                                    $this->wildDog_socket($shopid);
                                                    exit(json_encode(array('success' => true, 'results' => $reslut)));
                                                } else {
                                                    if ($operation == 'truntable') {
                                                        $otid = $_GPC['otid'];
                                                        $tid = $_GPC['tid'];
                                                        $targettable = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE  id =:a', array(':a' => $tid));
                                                        if ($targettable['status'] != 0) {
                                                            exit(json_encode(array('success' => false, 'msg' => '该台已开单无法转台')));
                                                        }
                                                        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and tableid=:a order by id desc limit 1', array(':a' => $otid));
                                                        pdo_update('j_hmoney_order', array('tableid' => $tid), array('id' => $trade['id']));
                                                        $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE  id =:a', array(':a' => $otid));
                                                        $data = array('status' => $table['status'], 'ispay' => $table['ispay'], 'orderid' => $table['orderid'], 'pauseorder' => $table['pauseorder'], 'ismore' => $table['ismore'], 'createtime' => $table['createtime'], 'endtime' => $table['endtime']);
                                                        pdo_update('j_hmoney_table', $data, array('id' => $tid));
                                                        $data2 = array('status' => 0, 'ispay' => 0, 'ismore' => 0, 'orderid' => 0, 'pauseorder' => 0, 'createtime' => 0, 'endtime' => 0);
                                                        pdo_update('j_hmoney_table', $data2, array('id' => $otid));
                                                        exit(json_encode(array('success' => true)));
                                                    } else {
                                                        if ($operation == 'getorderdetail2') {
                                                            $orderid = $_GPC['orderid'];
                                                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                            $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $trade['tableid']));
                                                            $goodlist = pdo_fetchall('SELECT a.*,b.memberprice,b.unitname,b.marketprice FROM ' . tablename('j_hmoney_ordergoods') . ' as a LEFT JOIN ' . tablename('j_hmoney_goods') . ' as b ON a.goodid=b.id  WHERE a.out_trade_no=:a order by a.id desc ', array(':a' => $trade['out_trade_no']));
                                                            exit(json_encode(array('success' => true, 'table' => $table, 'trade' => $trade, 'goods' => $goodlist)));
                                                        } else {
                                                            if ($operation == 'getorderdetail') {
                                                                $tid = $_GPC['tid'];
                                                                $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $tid));
                                                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and tableid=:a and ispay=0 order by id desc', array(':a' => $tid));
                                                                if (!$trade) {
                                                                    exit(json_encode(array('success' => false, 'msg' => '无订单处理')));
                                                                }
                                                                $goodlist = pdo_fetchall('SELECT a.*,b.memberprice,b.unitname,b.marketprice FROM ' . tablename('j_hmoney_ordergoods') . ' as a LEFT JOIN ' . tablename('j_hmoney_goods') . ' as b ON a.goodid=b.id  WHERE a.out_trade_no=:a order by a.id desc ', array(':a' => $trade['out_trade_no']));
                                                                exit(json_encode(array('success' => true, 'table' => $table, 'trade' => $trade, 'goods' => $goodlist)));
                                                            } else {
                                                                if ($operation == 'changeorder') {
                                                                    $orderid = $_GPC['orderid'];
                                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                                    $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $trade['tableid']));
                                                                    $person = intval($_GPC['person']);
                                                                    $teafee = intval(($person * $table['perperson'] + $table['teafee']) * 100);
                                                                    pdo_update('j_hmoney_order', array('person' => $person, 'teafee' => $teafee), array('id' => $orderid));
                                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                                    exit(json_encode(array('success' => true, 'trade' => $trade)));
                                                                } else {
                                                                    if ($operation == 'getorder') {
                                                                        $orderid = $_GPC['orderid'];
                                                                        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                                        $market = $this->findMarketing($trade['order_fee'] - $trade['notdiscount_fee'], $shopid, 1);
                                                                        exit(json_encode(array('success' => true, 'trade' => $trade, 'market' => $market)));
                                                                    } else {
                                                                        if ($operation == 'checkcoupon') {
                                                                            $code = $_GPC['code'];
                                                                            if (!$code) {
                                                                                exit(json_encode(array('success' => false, 'msg' => '卡券ID不能为空')));
                                                                            }
                                                                            load()->func('communication');
                                                                            $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account') . ' WHERE uniacid=:uniacid ', array(':uniacid' => $_W['uniacid']));
                                                                            $acc = WeAccount::create($acid);
                                                                            $tokens = $acc->fetch_token();
                                                                            $pageparama = json_encode(array('code' => $code));
                                                                            $resp = ihttp_request('https://api.weixin.qq.com/card/code/get?access_token=' . $tokens, $pageparama, $xml);
                                                                            if (is_error($resp)) {
                                                                                $procResult = $resp;
                                                                            }
                                                                            error_reporting(0);
                                                                            $result = json_decode($resp['content'], true);
                                                                            if ($result['errcode']) {
                                                                                exit(json_encode(array('success' => false, 'msg' => '卡券查询失败，原因：' . $result['errmsg'])));
                                                                            }
                                                                            $cardid = $result['card']['card_id'];
                                                                            $begin_time = $result['card']['begin_time'];
                                                                            $end_time = $result['card']['end_time'];
                                                                            $can_consume = $result['can_consume'];
                                                                            $user_card_status = $result['user_card_status'];
                                                                            $openid = $result['openid'];
                                                                            if ($begin_time > TIMESTAMP || $end_time < TIMESTAMP) {
                                                                                exit(json_encode(array('success' => false, 'msg' => '该卡券不可用，原因：该卡券使用时间为：' . date('Y-m-d H:i', $begin_time) . '-' . date('Y-m-d H:i', $end_time))));
                                                                            }
                                                                            $cardstatus = array('CONSUMED' => '已使用', 'EXPIRE' => '已过期', 'GIFT_TIMEOUT' => '转赠超时', 'DELETE' => '已删除', 'UNAVAILABLE' => '已失效', 'invalid serial code' => '已被朋友使用');
                                                                            if (!$can_consume) {
                                                                                exit(json_encode(array('success' => false, 'msg' => $cardstatus[$user_card_status])));
                                                                            }
                                                                            $pageparama = json_encode(array('card_id' => $cardid));
                                                                            $resp = ihttp_request('https://api.weixin.qq.com/card/get?access_token=' . $tokens, $pageparama, $xml);
                                                                            if (is_error($resp)) {
                                                                                exit(json_encode(array('success' => false, 'msg' => $resp)));
                                                                            }
                                                                            error_reporting(0);
                                                                            $cardinfo = json_decode($resp['content'], true);
                                                                            $coupon = array();
                                                                            $coupon['type'] = strtolower($cardinfo['card']['card_type']);
                                                                            switch ($coupon['type']) {
                                                                                case 'discount':
                                                                                    $coupon['typestr'] = '折扣券';
                                                                                    $coupon['discount'] = $cardinfo['card']['discount']['discount'];
                                                                                    $coupon['msg'] = $coupon['discount'] . '%';
                                                                                    break;
                                                                                case 'cash':
                                                                                    $coupon['typestr'] = '代金券';
                                                                                    $coupon['least_cost'] = isset($cardinfo['card']['cash']['least_cost']) ? $cardinfo['card']['cash']['least_cost'] : 0;
                                                                                    $coupon['discount'] = $cardinfo['card']['cash']['reduce_cost'];
                                                                                    if ($coupon['least_cost']) {
                                                                                    } else {
                                                                                    }
                                                                                    $coupon['msg'] = '满' . $this->_2money($coupon['least_cost']) . '元减' . $this->_2money($coupon['discount']) . '元';
                                                                                    break;
                                                                                case 'gift':
                                                                                    $coupon['typestr'] = '礼品券';
                                                                                    $coupon['msg'] = $cardinfo['card']['gift']['gift'];
                                                                                    break;
                                                                                case 'groupon':
                                                                                    $coupon['typestr'] = '团购券';
                                                                                    $coupon['msg'] = $cardinfo['card']['groupon']['deal_detail'];
                                                                                    break;
                                                                                case 'general_coupon':
                                                                                    $coupon['typestr'] = '优惠券';
                                                                                    $coupon['msg'] = $cardinfo['card']['general_coupon']['default_detail'];
                                                                                    break;
                                                                            }
                                                                            $coupon['openid'] = $openid;
                                                                            $coupon['code'] = $code;
                                                                            exit(json_encode(array('success' => true, 'cardinfo' => $coupon)));
                                                                        } else {
                                                                            if ($operation == 'getmember') {
                                                                                if (!$_GPC['memberid']) {
                                                                                    $keyword = trim($_GPC['keyword']);
                                                                                    if (!$keyword) {
                                                                                        exit(json_encode(array('success' => false, 'msg' => '请输入关键字')));
                                                                                    }
                                                                                    $condition = '  and (cardno =:a or wxcardno=:a or mobile=:a) ';
                                                                                    $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ' . $condition . ' ', array(':a' => $keyword));
                                                                                } else {
                                                                                    $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $_GPC['memberid']));
                                                                                }
                                                                                if (!$member) {
                                                                                    exit(json_encode(array('success' => false, 'msg' => '无此会员')));
                                                                                }
                                                                                $cost = pdo_fetchcolumn('SELECT sum(cash) FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and cid=:a and status=1 ', array(':a' => $member['id']));
                                                                                $member['cash2'] = $member['cash'];
                                                                                if ($member['birdday']) {
                                                                                } else {
                                                                                }
                                                                                $member['bird'] = date('m-d', strtotime($member['birdday']));
                                                                                $groupAry = array();
                                                                                mc_groups();
                                                                                foreach (mc_groups() as $row) {
                                                                                    $groupAry[$row['groupid']] = $row['title'];
                                                                                }
                                                                                $member['credit2'] = $member['credit'];
                                                                                $member['level'] = $groupAry[$member['gid']];
                                                                                if ($member['uid'] || $member['openid']) {
                                                                                    $uid = $member['uid'];
                                                                                    if (!$uid) {
                                                                                        $uid = mc_openid2uid($member['openid']);
                                                                                    }
                                                                                    $memberuser = mc_fetch($uid);
                                                                                    if ($cfg['isgeneralbalance']) {
                                                                                        intval($memberuser['credit2'] * 100);
                                                                                        $member['cash2'] = $member['cash2'] + intval($memberuser['credit2'] * 100);
                                                                                    }
                                                                                    $member['credit2'] = $member['credit'] + $memberuser['credit1'];
                                                                                }
                                                                                $member['cash2'] = $this->_2money($member['cash2']);
                                                                                $orderid = $_GPC['orderid'];
                                                                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                                                $market = $this->findMarketing($trade['order_fee'] - $trade['notdiscount_fee'], $shopid, 1, $member['id']);
                                                                                exit(json_encode(array('success' => true, 'member' => $member, 'market' => $market, 'cost' => $this->_2money($cost))));
                                                                            } else {
                                                                                if ($operation == 'sendmemberpaycode') {
                                                                                    $memberid = $_GPC['memberid'];
                                                                                    $orderid = $_GPC['orderid'];
                                                                                    $code = mt_rand(1000, 9999);
                                                                                    $member = pdo_fetch('SELECT openid FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $memberid));
                                                                                    if (!$member['openid']) {
                                                                                        exit(json_encode(array('success' => true, 'need' => false, 'code' => $code)));
                                                                                    }
                                                                                    if (!$cfg['tempmsg_paycode']) {
                                                                                        exit(json_encode(array('success' => true, 'need' => false, 'code' => $code)));
                                                                                    }
                                                                                    $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE groupid=:a and id=:b', array(':a' => $shopid, ':b' => $orderid));
                                                                                    pdo_update('j_hmoney_order', array('paycode' => $code), array('id' => $orderid));
                                                                                    $temp = array('first' => array('value' => '尊敬的顾客', 'color' => '#000000'), 'number' => array('value' => $code, 'color' => '#FF0000'), 'remark' => array('value' => '该验证码有效期3分钟可使用1次，转发无效', 'color' => '#333333'));
                                                                                    $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account') . ' WHERE uniacid=:uniacid ', array(':uniacid' => $_W['uniacid']));
                                                                                    $acc = WeAccount::create($acid);
                                                                                    $acc->sendTplNotice($member['openid'], $cfg['tempmsg_paycode'], $temp, '', '#FF0000');
                                                                                    exit(json_encode(array('success' => true, 'need' => true, 'code' => $code)));
                                                                                } else {
                                                                                    if ($operation == 'payall') {
                                                                                        $orderid = intval($_GPC['orderid']);
                                                                                        if (!$orderid) {
                                                                                            exit(json_encode(array('success' => false, 'msg' => '订单号不能为空')));
                                                                                        }
                                                                                        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                                        if ($trade['ispay'] > 0) {
                                                                                            exit(json_encode(array('success' => false, 'msg' => '已经支付的订单不能再支付')));
                                                                                        }
                                                                                        $data = array('total_fee' => intval($_GPC['totalfee'] * 100), 'paid_fee' => intval($_GPC['totalfee'] * 100), 'discount' => $_GPC['discount'], 'discount_fee' => intval($_GPC['coupon'] * 100), 'discounttype' => $_GPC['coupontype'], 'couponid' => $_GPC['couponno'], 'memberno' => $_GPC['memberid'], 'paytype' => $_GPC['paytype'], 'paytype2' => $_GPC['paytype2'], 'remark' => $_GPC['remark'], 'marketing' => $_GPC['marketing'], 'paid_fee1' => intval($_GPC['paidfee'] * 100), 'paid_fee2' => intval($_GPC['paidfee2'] * 100), 'change_fee' => intval($_GPC['change'] * 100), 'teafee' => $trade['teafee'] < intval($_GPC['teafee'] * 100) ? $trade['teafee'] : intval($_GPC['teafee'] * 100));
                                                                                        if ($data['paid_fee1'] + $data['paid_fee2'] > $data['total_fee']) {
                                                                                            if ($data['paytype'] == 2) {
                                                                                                $data['paid_fee1'] = $data['total_fee'] - $data['paid_fee2'];
                                                                                            }
                                                                                            if ($data['paytype2'] == 2) {
                                                                                                $data['paid_fee2'] = $data['total_fee'] - $data['paid_fee1'];
                                                                                            }
                                                                                        }
                                                                                        if ($data['teafee'] != $trade['teafee']) {
                                                                                            $data['remark'] = $trade['remark'] . ';免茶位';
                                                                                        }
                                                                                        if ($data['couponid']) {
                                                                                            $employCard = $this->employCard($data['couponid'], $shopid);
                                                                                            if (!$employCard || !$employCard['success']) {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '卡券核销失败：' . $employCard['msg'])));
                                                                                            }
                                                                                        }
                                                                                        if (!$data['paid_fee2']) {
                                                                                            $data['paytype2'] = -1;
                                                                                            $data['pay2_status'] = 1;
                                                                                        }
                                                                                        pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                                                                        if ($data['paytype'] > 4 || $data['paytype2'] > 4) {
                                                                                            $data['ispay'] = 1;
                                                                                            $data['time_end'] = TIMESTAMP;
                                                                                            if ($data['paytype'] > 4) {
                                                                                                $data['pay1_status'] = 1;
                                                                                            }
                                                                                            if ($data['paytype2'] > 4) {
                                                                                                $data['pay2_status'] = 1;
                                                                                            }
                                                                                        }
                                                                                        if ($data['paytype'] == 2 || $data['paytype2'] == 2) {
                                                                                            $data['ispay'] = 1;
                                                                                            $data['time_end'] = TIMESTAMP;
                                                                                            if ($data['paytype'] == 2) {
                                                                                                $data['pay1_status'] = 1;
                                                                                            }
                                                                                            if ($data['paytype2'] == 2) {
                                                                                                $data['pay2_status'] = 1;
                                                                                            }
                                                                                        }
                                                                                        if ($data['paytype'] == 3 || $data['paytype2'] == 3) {
                                                                                            $data['ispay'] = 1;
                                                                                            $data['time_end'] = TIMESTAMP;
                                                                                            $data['remark'] = $data['remark'] . ';银行卡' . $_GPC['code'];
                                                                                            if ($data['paytype'] == 3) {
                                                                                                $data['pay1_status'] = 1;
                                                                                            }
                                                                                            if ($data['paytype2'] == 3) {
                                                                                                $data['pay2_status'] = 1;
                                                                                            }
                                                                                        }
                                                                                        if ($data['paytype'] == 4 || $data['paytype2'] == 4) {
                                                                                            if (!$data['memberno']) {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '会员卡不能为空')));
                                                                                            }
                                                                                            $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $data['memberno']));
                                                                                            if (!$member) {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '会员数据错误')));
                                                                                            }
                                                                                            if ($cfg['ispaycode']) {
                                                                                                if (!$_GPC['paycode']) {
                                                                                                    exit(json_encode(array('success' => false, 'msg' => '验证码不能为空')));
                                                                                                }
                                                                                                if ($_GPC['paycode'] != $trade['paycode']) {
                                                                                                    exit(json_encode(array('success' => false, 'msg' => '验证码错误')));
                                                                                                }
                                                                                            }
                                                                                            $membercash = $member['cash'];
                                                                                            $isGeneral = 0;
                                                                                            if ($member['openid'] && $cfg['isgeneralbalance']) {
                                                                                                if ($member['cash']) {
                                                                                                    $this->transferBalance($member['id']);
                                                                                                }
                                                                                                $uid = $member['uid'];
                                                                                                if (!$uid) {
                                                                                                    $uid = mc_openid2uid($member['openid']);
                                                                                                }
                                                                                                $memberinfo = mc_fetch($uid);
                                                                                                $membercash = intval($memberinfo['credit2'] * 100);
                                                                                                $isGeneral = 1;
                                                                                            }
                                                                                            $payfee = $data['paytype'] == 4 ? $data['paid_fee1'] : $data['paid_fee2'];
                                                                                            if ($membercash < $payfee) {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '余额不足')));
                                                                                            }
                                                                                            if ($isGeneral) {
                                                                                                $this->_2money($payfee);
                                                                                                mc_credit_update($uid, 'credit2', $this->_2money($payfee) * -1, array(0 => '', 1 => '收银系统支付￥' . $this->_2money($payfee) . '元'));
                                                                                            } else {
                                                                                                pdo_update('j_hmoney_membercard', array('cash' => $member['cash'] - $payfee), array('id' => $member['id']));
                                                                                            }
                                                                                            $this->memberCardInform($data['memberno']);
                                                                                            $data['ispay'] = 1;
                                                                                            $data['time_end'] = TIMESTAMP;
                                                                                            if ($data['paytype'] == 4) {
                                                                                                $data['pay1_status'] = 1;
                                                                                            }
                                                                                            if ($data['paytype2'] == 4) {
                                                                                                $data['pay2_status'] = 1;
                                                                                            }
                                                                                            $memberdata = array('weid' => $_W['uniacid'], 'cid' => $member['id'], 'userid' => $userid, 'groupid' => $shopid, 'usetype' => 1, 'paytype' => -1, 'startfee' => $membercash, 'cash' => $payfee, 'endfee' => $membercash - $payfee, 'out_trade_no' => $trade['out_trade_no'], 'status' => 1, 'createtime' => TIMESTAMP, 'createdate' => date('Y-m-d'));
                                                                                            pdo_insert('j_hmoney_recharge', $memberdata);
                                                                                        }
                                                                                        if ($data['paytype'] == 0 || $data['paytype2'] == 0) {
                                                                                            $qrcode = $_GPC['qrcode'];
                                                                                            $payfee = $data['paytype'] == 0 ? $data['paid_fee1'] : $data['paid_fee2'];
                                                                                            if ($cfg['isbank']) {
                                                                                                $bank_trade_no = TIMESTAMP . mt_rand(1000, 9999);
                                                                                                pdo_update('j_hmoney_order', array('bank_trade_no' => $bank_trade_no), array('id' => $orderid));
                                                                                                $result = Bankpay_micropay($cfg, $payfee, $bank_trade_no, $trade['out_trade_no'], $qrcode, $userid, $shopid);
                                                                                                if (isset($result['success']) && $result['success'] == false) {
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                                                }
                                                                                                if ($result['status'] || $result['result_code']) {
                                                                                                    if ($result['err_code'] == 10003 || $result['err_code'] == 'USERPAYING') {
                                                                                                        exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                                                    }
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['message'] . $result['err_msg'])));
                                                                                                }
                                                                                                $data['openid'] = $result['sub_openid'];
                                                                                                $data['cash_fee'] = $result['total_fee'];
                                                                                                $data['transaction_id'] = $result['out_transaction_id'];
                                                                                                $data['time_end'] = strtotime($result['time_end']);
                                                                                                $data['ispay'] = 1;
                                                                                            } else {
                                                                                                if ($cfg['isymf']) {
                                                                                                    $payfee1 = $this->_2money($payfee);
                                                                                                    $result = ymf_micropay($qrcode, $payfee1, $trade['id'], $cfg);
                                                                                                    if ($result['success']) {
                                                                                                        $data['bank_trade_no'] = $result['orderno'];
                                                                                                        $data['openid'] = $result['uuid'];
                                                                                                        $data['cash_fee'] = intval($result['money'] * 100);
                                                                                                        $data['transaction_id'] = $result['transid'];
                                                                                                        $data['time_end'] = strtotime($result['paytime']);
                                                                                                        $data['ispay'] = 1;
                                                                                                    } else {
                                                                                                        exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                                                    }
                                                                                                } else {
                                                                                                    $payParama = array('appid' => $shop['appid'] ? $shop['appid'] : $cfg['appid'], 'pay_mchid' => strval($shop['pay_mchid']) ? strval($shop['pay_mchid']) : strval($cfg['pay_mchid']), 'pay_signkey' => $shop['pay_signkey'] ? $shop['pay_signkey'] : $cfg['pay_signkey'], 'outTradeNo' => strval($trade['out_trade_no']), 'pay_ip' => $shop['pay_ip'] ? $shop['pay_ip'] : $cfg['pay_ip'], 'sub_appid' => $shop['sub_appid'] ? $shop['sub_appid'] : $cfg['sub_appid'], 'sub_mch_id' => $shop['sub_mch_id'] ? $shop['sub_mch_id'] : $cfg['sub_mch_id']);
                                                                                                    $pay_signkey = trim($shop['pay_signkey']) ? trim($shop['pay_signkey']) : trim($cfg['pay_signkey']);
                                                                                                    $pageparama = array('appid' => $shop['appid'] ? $shop['appid'] : $cfg['appid'], 'mch_id' => strval($shop['pay_mchid']) ? strval($shop['pay_mchid']) : strval($cfg['pay_mchid']), 'device_info' => $userid, 'nonce_str' => getNonceStr(), 'body' => $shop['companyname'], 'detail' => $shop['companyname'], 'attach' => '-', 'out_trade_no' => $payParama['outTradeNo'], 'total_fee' => $payfee, 'fee_type' => 'CNY', 'spbill_create_ip' => $shop['pay_ip'] ? $shop['pay_ip'] : $cfg['pay_ip'], 'goods_tag' => '000001', 'auth_code' => $qrcode);
                                                                                                    if ($payParama['sub_appid']) {
                                                                                                        $pageparama['sub_appid'] = $payParama['sub_appid'];
                                                                                                    }
                                                                                                    if ($payParama['sub_mch_id']) {
                                                                                                        $pageparama['sub_mch_id'] = $payParama['sub_mch_id'];
                                                                                                    }
                                                                                                    $sign = MakeSign($pageparama, $pay_signkey);
                                                                                                    $pageparama['sign'] = $sign;
                                                                                                    $xml = ToXml($pageparama);
                                                                                                    $response = postXmlCurl($xml, 'https://api.mch.weixin.qq.com/pay/micropay', 10);
                                                                                                    $result = FromXml($response);
                                                                                                    if ($result['result_code'] != 'SUCCESS') {
                                                                                                        if ($result['err_code'] == 'USERPAYING') {
                                                                                                            exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                                                        } else {
                                                                                                            if ($result['err_code'] == 'INVALID_REQUEST') {
                                                                                                                if ($result['err_code_des'] == 201) {
                                                                                                                    $neworderno = TIMESTAMP . mt_rand(1, 999);
                                                                                                                    pdo_update('j_hmoney_order', array('out_trade_no' => $neworderno), array('id' => $orderid));
                                                                                                                    pdo_update('j_hmoney_ordergoods', array('out_trade_no' => $neworderno), array('orderid' => $orderid));
                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '支付单号重复，请再次提交')));
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                        exit(json_encode(array('success' => false, 'msg' => $result['err_code_des'] . '-' . $result['return_msg'])));
                                                                                                    }
                                                                                                    $data['openid'] = $result['openid'];
                                                                                                    $data['cash_fee'] = $result['cash_fee'];
                                                                                                    $data['coupon_fee'] = $result['coupon_fee'];
                                                                                                    $data['transaction_id'] = $result['transaction_id'];
                                                                                                    $data['time_end'] = strtotime($result['time_end']);
                                                                                                    $data['ispay'] = 1;
                                                                                                }
                                                                                            }
                                                                                            if ($data['paytype'] == 0) {
                                                                                                $data['pay1_status'] = 1;
                                                                                            }
                                                                                            if ($data['paytype2'] == 0) {
                                                                                                $data['pay2_status'] = 1;
                                                                                            }
                                                                                        }
                                                                                        if ($data['paytype'] == 1 || $data['paytype2'] == 1) {
                                                                                            $qrcode = $_GPC['qrcode'];
                                                                                            $payfee = $data['paytype'] == 1 ? $data['paid_fee1'] : $data['paid_fee2'];
                                                                                            if ($cfg['isbank']) {
                                                                                                $bank_trade_no = TIMESTAMP . mt_rand(1000, 9999);
                                                                                                pdo_update('j_hmoney_order', array('bank_trade_no' => $bank_trade_no), array('id' => $orderid));
                                                                                                $result = Bankpay_micropay($cfg, $payfee, $bank_trade_no, $trade['out_trade_no'], $qrcode, $userid, $shopid);
                                                                                                if (isset($result['success']) && $result['success'] == false) {
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                                                }
                                                                                                if ($result['status'] || $result['result_code']) {
                                                                                                    if ($result['err_code'] == 10003 || $result['err_code'] == 'USERPAYING') {
                                                                                                        exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                                                    }
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['message'] . $result['err_msg'])));
                                                                                                }
                                                                                                $data['openid'] = $result['sub_openid'];
                                                                                                $data['cash_fee'] = $result['total_fee'];
                                                                                                $data['transaction_id'] = $result['out_transaction_id'];
                                                                                                $data['time_end'] = strtotime($result['time_end']);
                                                                                                $data['ispay'] = 1;
                                                                                            } else {
                                                                                                if ($cfg['isymf']) {
                                                                                                    $payfee1 = $this->_2money($payfee);
                                                                                                    $result = ymf_micropay($qrcode, $payfee1, $trade['id'], $cfg);
                                                                                                    if ($result['success']) {
                                                                                                        $data['bank_trade_no'] = $result['orderno'];
                                                                                                        $data['openid'] = $result['uuid'];
                                                                                                        $data['cash_fee'] = intval($result['money'] * 100);
                                                                                                        $data['transaction_id'] = $result['transid'];
                                                                                                        $data['time_end'] = strtotime($result['paytime']);
                                                                                                        $data['ispay'] = 1;
                                                                                                    } else {
                                                                                                        exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                                                    }
                                                                                                } else {
                                                                                                    $payParama = array('app_id' => $shop['app_id'] ? $shop['app_id'] : $cfg['app_id'], 'outTradeNo' => strval($trade['out_trade_no']), 'appauthtoken' => $shop['appauthtoken'] ? $shop['appauthtoken'] : $cfg['appauthtoken'], 'alipay_cert' => $cfg['alipay_cert'], 'alipay_key' => $cfg['alipay_key'], 'alipay_store_id' => $shop['alipay_store_id'], 'alipid' => $shop['alipid'] ? $shop['alipid'] : $cfg['alipid']);
                                                                                                    $total_amount = $data['total_fee'];
                                                                                                    $postfee = $this->_2money($payfee);
                                                                                                    require_once '../addons/j_hmoney/plug/plug_alipay/F2fpay.php';
                                                                                                    $f2fpay = new F2fpay();
                                                                                                    $response = $f2fpay->barpay($payParama['outTradeNo'], $qrcode, $postfee, $shop['companyname'], $payParama);
                                                                                                    $temp = (array) $response;
                                                                                                    $result = (array) $temp['alipay_trade_pay_response'];
                                                                                                    if ($result['code'] == 10003) {
                                                                                                        exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                                                    } else {
                                                                                                        if ($result['code'] == '10000') {
                                                                                                            $data['transaction_id'] = $result['trade_no'];
                                                                                                            $data['cash_fee'] = intval($result['receipt_amount'] * 100);
                                                                                                            $data['time_end'] = strtotime($result['gmt_payment']);
                                                                                                            $data['ispay'] = 1;
                                                                                                        } else {
                                                                                                            exit(json_encode(array('success' => false, 'msg' => $result['sub_msg'], 'aa' => $auth_code)));
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                            if ($data['paytype'] == 1) {
                                                                                                $data['pay1_status'] = 1;
                                                                                            }
                                                                                            if ($data['paytype2'] == 1) {
                                                                                                $data['pay2_status'] = 1;
                                                                                            }
                                                                                        }
                                                                                        $data['status'] = 2;
                                                                                        pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                                                                        pdo_update('j_hmoney_ordergoods', array('ispay' => 1), array('orderid' => $orderid));
                                                                                        pdo_update('j_hmoney_table', array('ispay' => 1, 'newctrl' => 0, 'endtime' => TIMESTAMP), array('id' => $trade['tableid']));
                                                                                        $this->wildDog_socket($shopid);
                                                                                        exit(json_encode(array('success' => true)));
                                                                                    } else {
                                                                                        if ($operation == 'paywaitpassword') {
                                                                                            $orderid = intval($_GPC['orderid']);
                                                                                            if (!$orderid) {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '订单号不能为空')));
                                                                                            }
                                                                                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                                            if ($trade['ispay'] == 1) {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '已经支付的订单不能再支付')));
                                                                                            }
                                                                                            if ($cfg['isbank']) {
                                                                                                $result = Bankpay_checkpay($cfg, $trade['bank_trade_no']);
                                                                                                if (!$result['status'] && !$result['result_code']) {
                                                                                                    if ($result['trade_state'] == 'USERPAYING' || $result['trade_state'] == 10003) {
                                                                                                        exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                                                    } else {
                                                                                                        if ($result['trade_state'] == 'SUCCESS') {
                                                                                                            $data = array('openid' => $result['sub_openid'], 'ispay' => 1, 'status' => 2, 'cash_fee' => intval($result['total_fee']), 'transaction_id' => $result['out_transaction_id'], 'time_end' => strtotime($result['time_end']));
                                                                                                            if ($result['trade_type'] == 'pay.weixin.micropay') {
                                                                                                                if ($trade['paytype'] == 0) {
                                                                                                                    $data['pay1_status'] = 1;
                                                                                                                }
                                                                                                                if ($trade['paytype2'] == 0) {
                                                                                                                    $data['pay2_status'] = 1;
                                                                                                                }
                                                                                                            } else {
                                                                                                                if ($trade['paytype'] == 1) {
                                                                                                                    $data['pay1_status'] = 1;
                                                                                                                }
                                                                                                                if ($trade['paytype2'] == 1) {
                                                                                                                    $data['pay2_status'] = 1;
                                                                                                                }
                                                                                                            }
                                                                                                            pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                                                                                                            pdo_update('j_hmoney_ordergoods', array('ispay' => 1), array('orderid' => $trade['id']));
                                                                                                            pdo_update('j_hmoney_table', array('ispay' => 1), array('id' => $trade['tableid']));
                                                                                                            $this->wildDog_socket($shopid);
                                                                                                            exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => false, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                        }
                                                                                                    }
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['trade_state'])));
                                                                                                }
                                                                                                exit(json_encode(array('success' => false, 'msg' => $result['message'] . $result['err_msg'])));
                                                                                            } else {
                                                                                                load()->func('communication');
                                                                                                if ($trade['paytype'] == 0) {
                                                                                                    $payParama = array('appid' => $shop['appid'] ? $shop['appid'] : $cfg['appid'], 'pay_mchid' => strval($shop['pay_mchid']) ? strval($shop['pay_mchid']) : strval($cfg['pay_mchid']), 'pay_signkey' => $shop['pay_signkey'] ? $shop['pay_signkey'] : $cfg['pay_signkey'], 'outTradeNo' => strval($trade['out_trade_no']), 'pay_ip' => $shop['pay_ip'] ? $shop['pay_ip'] : $cfg['pay_ip'], 'sub_appid' => $shop['sub_appid'] ? $shop['sub_appid'] : $cfg['sub_appid'], 'sub_mch_id' => $shop['sub_mch_id'] ? $shop['sub_mch_id'] : $cfg['sub_mch_id']);
                                                                                                    $pageparama = array('appid' => $payParama['appid'], 'mch_id' => strval($payParama['pay_mchid']), 'out_trade_no' => $trade['out_trade_no'], 'nonce_str' => getNonceStr());
                                                                                                    if ($payParama['sub_appid']) {
                                                                                                        $pageparama['sub_appid'] = $payParama['sub_appid'];
                                                                                                    }
                                                                                                    if ($payParama['sub_mch_id']) {
                                                                                                        $pageparama['sub_mch_id'] = $payParama['sub_mch_id'];
                                                                                                    }
                                                                                                    $sign = MakeSign($pageparama, $payParama['pay_signkey']);
                                                                                                    $pageparama['sign'] = $sign;
                                                                                                    $xml = ToXml($pageparama);
                                                                                                    $response = postXmlCurl($xml, 'https://api.mch.weixin.qq.com/pay/orderquery', 10);
                                                                                                    $result = FromXml($response);
                                                                                                    if ($result['result_code'] == 'SUCCESS') {
                                                                                                        if ($result['trade_state'] == 'USERPAYING') {
                                                                                                            exit(json_encode(array('success' => true, 'paywait' => true, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                        } else {
                                                                                                            if ($result['trade_state'] == 'REFUND') {
                                                                                                                exit(json_encode(array('success' => false, 'msg' => '已退款')));
                                                                                                            } else {
                                                                                                                if ($result['trade_state'] == 'NOTPAY') {
                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '没有支付')));
                                                                                                                } else {
                                                                                                                    if ($result['trade_state'] == 'CLOSED') {
                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '已关闭')));
                                                                                                                    } else {
                                                                                                                        if ($result['trade_state'] == 'REVOKED') {
                                                                                                                            exit(json_encode(array('success' => false, 'msg' => '已撤销')));
                                                                                                                        } else {
                                                                                                                            if ($result['trade_state'] == 'PAYERROR') {
                                                                                                                                exit(json_encode(array('success' => false, 'msg' => '支付失败(银行返回失败)')));
                                                                                                                            } else {
                                                                                                                                if ($result['trade_state'] == 'SUCCESS') {
                                                                                                                                    $data = array('openid' => $result['openid'], 'ispay' => 1, 'status' => 2, 'coupon_fee' => intval($result['coupon_fee']), 'cash_fee' => intval($result['cash_fee']), 'transaction_id' => $result['transaction_id'], 'time_end' => strtotime($result['time_end']));
                                                                                                                                    if ($trade['paytype'] == 0) {
                                                                                                                                        $data['pay1_status'] = 1;
                                                                                                                                    }
                                                                                                                                    if ($trade['paytype2'] == 0) {
                                                                                                                                        $data['pay2_status'] = 1;
                                                                                                                                    }
                                                                                                                                    pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                                                                                                                                    pdo_update('j_hmoney_ordergoods', array('ispay' => 1), array('orderid' => $trade['id']));
                                                                                                                                    pdo_update('j_hmoney_table', array('ispay' => 1), array('id' => $trade['tableid']));
                                                                                                                                    $this->wildDog_socket($shopid);
                                                                                                                                    if ($trade['ispay'] == 0) {
                                                                                                                                        exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => true, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                                                    }
                                                                                                                                    exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => false, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['trade_state_desc'] . $result['err_code_des'] . $result['return_msg'])));
                                                                                                } else {
                                                                                                    $payParama = array('app_id' => $shop['app_id'] ? $shop['app_id'] : $cfg['app_id'], 'appauthtoken' => $shop['appauthtoken'] ? $shop['appauthtoken'] : $cfg['appauthtoken'], 'alipay_cert' => $cfg['alipay_cert'], 'alipay_key' => $cfg['alipay_key'], 'alipay_store_id' => $shop['alipay_store_id'], 'alipid' => $cfg['alipid']);
                                                                                                    require_once '../addons/j_hmoney/plug/plug_alipay/F2fpay.php';
                                                                                                    $cfg = $this->module['config'];
                                                                                                    $f2fpay = new F2fpay();
                                                                                                    $response = $f2fpay->query($out_trade_no, $payParama);
                                                                                                    error_reporting(0);
                                                                                                    $results = json_decode(json_encode($response), true);
                                                                                                    $result = $results['alipay_trade_query_response'];
                                                                                                    if ($result['code'] == 10003) {
                                                                                                        exit(json_encode(array('success' => true, 'paywait' => true, 'msg' => '等待客户支付密码', 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                    } else {
                                                                                                        if ($result['code'] == 10000) {
                                                                                                            if ($result['trade_status'] == 'TRADE_SUCCESS') {
                                                                                                                $data = array('ispay' => 1, 'status' => 2, 'transaction_id' => $result['trade_no'], 'cash_fee' => intval($result['receipt_amount'] * 100), 'time_end' => strtotime($result['gmt_payment']));
                                                                                                                if ($trade['paytype'] == 1) {
                                                                                                                    $data['pay1_status'] = 1;
                                                                                                                }
                                                                                                                if ($trade['paytype2'] == 1) {
                                                                                                                    $data['pay2_status'] = 1;
                                                                                                                }
                                                                                                                pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                                                                                                                pdo_update('j_hmoney_ordergoods', array('ispay' => 1), array('orderid' => $trade['id']));
                                                                                                                pdo_update('j_hmoney_table', array('ispay' => 1), array('id' => $trade['tableid']));
                                                                                                                $isnew = false;
                                                                                                                if ($trade['ispay'] == 0) {
                                                                                                                    $isnew = true;
                                                                                                                }
                                                                                                                exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => $isnew, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                            } else {
                                                                                                                if ($result['trade_status'] == 'WAIT_BUYER_PAY') {
                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '等待客户付款')));
                                                                                                                } else {
                                                                                                                    if ($result['trade_status'] == 'TRADE_CLOSED') {
                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '订单已退款')));
                                                                                                                    } else {
                                                                                                                        exit(json_encode(array('success' => false, 'msg' => $result['trade_status'])));
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['sub_msg'])));
                                                                                                }
                                                                                            }
                                                                                        } else {
                                                                                            if ($operation == 'getorderlist') {
                                                                                                $datetype = intval($_GPC['datetype']);
                                                                                                $keyword = trim($_GPC['keyword']);
                                                                                                switch ($datetype) {
                                                                                                    case 0:
                                                                                                        break;
                                                                                                    case 1:
                                                                                                        $condition = ' and a.createdate=\'' . date('Y-m-d') . '\' ';
                                                                                                        break;
                                                                                                    case 2:
                                                                                                        $condition = ' and a.createdate=\'' . date('Y-m-d', strtotime('-1 day')) . '\' ';
                                                                                                        break;
                                                                                                    case 3:
                                                                                                        $condition = ' and a.createdate>=\'' . date('Y-m-d', strtotime('-3 day')) . '\' ';
                                                                                                        break;
                                                                                                    case 4:
                                                                                                        $condition = ' and a.createdate>=\'' . date('Y-m-d', strtotime('-7 day')) . '\' ';
                                                                                                        break;
                                                                                                    case 5:
                                                                                                        $condition = ' and date_format(a.createdate,\'%y-%m\')=\'' . date('y-m') . '\' ';
                                                                                                        break;
                                                                                                }
                                                                                                if ($keyword) {
                                                                                                    $condition = ' and a.out_trade_no=\'' . $keyword . '\' ';
                                                                                                }
                                                                                                $condition .= ' and a.groupid =\'' . $shopid . '\' ';
                                                                                                $sql = 'SELECT a.id,a.out_trade_no,a.createtime,b.title FROM ' . tablename('j_hmoney_order') . ' as a left join ' . tablename('j_hmoney_table') . ' as b on a.tableid=b.id WHERE a.weid = \'' . $_W['uniacid'] . '\' ' . $condition . ' order by a.id desc ';
                                                                                                $list = pdo_fetchall($sql);
                                                                                                exit(json_encode(array('success' => true, 'list' => $list, 'sql' => $sql)));
                                                                                            } else {
                                                                                                if ($operation == 'getorderhistroy') {
                                                                                                    $datetype = intval($_GPC['datetype']);
                                                                                                    $keyword = trim($_GPC['keyword']);
                                                                                                    switch ($datetype) {
                                                                                                        case 0:
                                                                                                            break;
                                                                                                        case 1:
                                                                                                            $condition = ' and a.createdate=\'' . date('Y-m-d') . '\' ';
                                                                                                            break;
                                                                                                        case 2:
                                                                                                            $condition = ' and a.createdate=\'' . date('Y-m-d', strtotime('-1 day')) . '\' ';
                                                                                                            break;
                                                                                                        case 3:
                                                                                                            $condition = ' and a.createdate>=\'' . date('Y-m-d', strtotime('-3 day')) . '\' ';
                                                                                                            break;
                                                                                                        case 4:
                                                                                                            $condition = ' and a.createdate>=\'' . date('Y-m-d', strtotime('-7 day')) . '\' ';
                                                                                                            break;
                                                                                                        case 5:
                                                                                                            $condition = ' and date_format(a.createdate,\'%y-%m\')=\'' . date('y-m') . '\' ';
                                                                                                            break;
                                                                                                    }
                                                                                                    if ($keyword) {
                                                                                                        $condition = ' and (a.out_trade_no=\'' . $keyword . '\' or b.title like \'%' . $keyword . '%\') and a.createdate=\'' . date('Y-m-d') . '\' ';
                                                                                                    }
                                                                                                    $condition .= ' and a.groupid =\'' . $shopid . '\' ';
                                                                                                    $sql = 'SELECT a.id,a.out_trade_no,a.createtime,b.title FROM ' . tablename('j_hmoney_order') . ' as a left join ' . tablename('j_hmoney_table') . ' as b on a.tableid=b.id WHERE a.weid = \'' . $_W['uniacid'] . '\' ' . $condition . ' order by a.id desc ';
                                                                                                    $list = pdo_fetchall($sql);
                                                                                                    exit(json_encode(array('success' => true, 'list' => $list, 'sql' => $sql)));
                                                                                                } else {
                                                                                                    if ($operation == 'gethistoryview') {
                                                                                                        $tid = $_GPC['id'];
                                                                                                        $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $tid));
                                                                                                        $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $order['tableid']));
                                                                                                        $goods = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and orderid=:a order by id asc', array(':a' => $order['id']));
                                                                                                        exit(json_encode(array('success' => true, 'trade' => $order, 'table' => $table, 'goods' => $goods)));
                                                                                                    } else {
                                                                                                        if ($operation == 'getordergood') {
                                                                                                            $gid = $_GPC['id'];
                                                                                                            $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $gid));
                                                                                                            if ($goods['ispay'] != 1) {
                                                                                                                exit(json_encode(array('success' => false, 'msg' => '该商品不能退款')));
                                                                                                            }
                                                                                                            $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $goods['orderid']));
                                                                                                            $maxfee = $goods['price'];
                                                                                                            if ($order['discount'] != '100%') {
                                                                                                                str_replace('%', '', $order['discount']);
                                                                                                                $maxfee = $this->_2money($goods['price'] * str_replace('%', '', $order['discount']));
                                                                                                            }
                                                                                                            $paytypestr = '微信';
                                                                                                            if ($order['paytype'] == 1) {
                                                                                                                $paytypestr = '支付宝';
                                                                                                            } else {
                                                                                                                if ($order['paytype'] == 2) {
                                                                                                                    $paytypestr = '现金';
                                                                                                                } else {
                                                                                                                    if ($order['paytype'] == 3) {
                                                                                                                        $paytypestr = '银行卡';
                                                                                                                    } else {
                                                                                                                        if ($order['paytype'] == 4) {
                                                                                                                            $paytypestr = '会员卡';
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            exit(json_encode(array('success' => true, 'maxfee' => $maxfee, 'good' => $goods, 'paytype' => $order['paytype'], 'paytypestr' => $paytypestr)));
                                                                                                        } else {
                                                                                                            if ($operation == 'getgood') {
                                                                                                                $id = $_GPC['id'];
                                                                                                                $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE id = :a ', array(':a' => $id));
                                                                                                                exit(json_encode(array('success' => true, 'goods' => $goods)));
                                                                                                            } else {
                                                                                                                if ($operation == 'getgiftlist') {
                                                                                                                    $giftlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_gift') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by status desc, id desc');
                                                                                                                    exit(json_encode(array('success' => true, 'list' => $giftlist)));
                                                                                                                } else {
                                                                                                                    if ($operation == 'getgiftrecord') {
                                                                                                                        $pindex = max(1, intval($_GPC['page']));
                                                                                                                        $psize = 5;
                                                                                                                        $condition = ' and a.groupid =\'' . $shopid . '\' ';
                                                                                                                        if ($_GPC['keyword']) {
                                                                                                                            $condition .= ' and ( a.memberid=\'' . $_GPC['keyword'] . '\' )';
                                                                                                                        }
                                                                                                                        $list = pdo_fetchall('SELECT a.*,b.realname,b.cardno FROM ' . tablename('j_hmoney_giftrecord') . ' a left join ' . tablename('j_hmoney_membercard') . ' b on a.memberid=b.id WHERE a.weid = \'' . $_W['uniacid'] . '\' ' . $condition . ' order by a.id desc LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
                                                                                                                        $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_giftrecord') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ' . $condition);
                                                                                                                        $pageall = $total <= $psize ? 1 : ($total % $psize ? $total / $psize + 1 : $total / $psize);
                                                                                                                        $userlist = pdo_fetchall('SELECT id,realname FROM ' . tablename('j_hmoney_user') . ' WHERE pcate =:a', array(':a' => $shopid));
                                                                                                                        $userary = array();
                                                                                                                        foreach ($userlist as $row) {
                                                                                                                            $userary[$row['id']] = $row['realname'];
                                                                                                                        }
                                                                                                                        $i = 0;
                                                                                                                        while ($i < count($list)) {
                                                                                                                            $list[$i]['username'] = $userary[$list[$i]['userid']];
                                                                                                                            $i = $i + 1;
                                                                                                                        }
                                                                                                                        exit(json_encode(array('success' => true, 'list' => $list, 'pindex' => $pindex, 'total' => $total)));
                                                                                                                    } else {
                                                                                                                        if ($operation == 'exchangegift') {
                                                                                                                            $fid = $_GPC['id'];
                                                                                                                            $memberid = $_GPC['mid'];
                                                                                                                            $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $memberid));
                                                                                                                            $member['credit2'] = $member['credit'];
                                                                                                                            $uid = $member['uid'];
                                                                                                                            if ($uid) {
                                                                                                                                $this->memberCardInform($memberid);
                                                                                                                                $memberuser = mc_fetch($uid);
                                                                                                                                $member['credit2'] = $memberuser['credit1'];
                                                                                                                            }
                                                                                                                            $gift = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_gift') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $fid));
                                                                                                                            if (intval($gift['credit'] * 100) > intval($member['credit2'] * 100)) {
                                                                                                                                exit(json_encode(array('success' => false, 'msg' => '积分不足')));
                                                                                                                            }
                                                                                                                            $datagift = array();
                                                                                                                            if ($gift['store'] > 0) {
                                                                                                                                if ($gift['remain'] == 0) {
                                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '礼品数量不足')));
                                                                                                                                }
                                                                                                                                $datagift['remain'] = $gift['remain'] - 1;
                                                                                                                            }
                                                                                                                            pdo_update('j_hmoney_gift', $datagift, array('id' => $fid));
                                                                                                                            $outTradeno = TIMESTAMP;
                                                                                                                            $data = array('weid' => $_W['uniacid'], 'userid' => $userid, 'groupid' => $shopid, 'memberid' => $memberid, 'credit' => $gift['credit'], 'out_trade_no' => $outTradeno, 'giftid' => $gift['id'], 'title' => $gift['title'], 'gtype' => $gift['gtype'], 'status' => '', 'createtime' => TIMESTAMP);
                                                                                                                            pdo_insert('j_hmoney_giftrecord', $data);
                                                                                                                            $tid = pdo_insertid();
                                                                                                                            $datas = array();
                                                                                                                            if (NULL == 0) {
                                                                                                                                $datas['status'] = 1;
                                                                                                                            } else {
                                                                                                                                if (NULL == 1) {
                                                                                                                                    $content = $gift['content'];
                                                                                                                                    $favorAry = strpos($content, '|') ? explode('|', $content) : array(0 => $content);
                                                                                                                                    shuffle($favorAry);
                                                                                                                                    $cardkey = $favorAry[0];
                                                                                                                                    $datas['content'] = $cardkey;
                                                                                                                                    $url = $this->IncreateQrcode($outTradeno, 1);
                                                                                                                                    $datas['qrcode'] = $url;
                                                                                                                                } else {
                                                                                                                                    if (NULL == 2) {
                                                                                                                                        $content = $gift['content'];
                                                                                                                                        if (strpos($content, '-')) {
                                                                                                                                            $temp = explode('|', $content);
                                                                                                                                            $cash = mt_rand($temp[0] * 100, $temp[1] * 100);
                                                                                                                                        } else {
                                                                                                                                            $cash = $content * 100;
                                                                                                                                        }
                                                                                                                                        $datas['content'] = $cash;
                                                                                                                                        $url = $this->IncreateQrcode($outTradeno, 1);
                                                                                                                                        $datas['qrcode'] = $url;
                                                                                                                                    } else {
                                                                                                                                        if (null == 3) {
                                                                                                                                            $content = $gift['content'];
                                                                                                                                            $datas['content'] = $content;
                                                                                                                                            pdo_update('j_hmoney_membercard', array('cash' => $member['cash'] + $content * 100), array('id' => $memberid));
                                                                                                                                            $datas['status'] = 1;
                                                                                                                                            $datas['isget'] = 1;
                                                                                                                                            $datas['endtime'] = TIMESTAMP;
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                            pdo_update('j_hmoney_giftrecord', $datas, array('out_trade_no' => $outTradeno));
                                                                                                                            if (!$uid) {
                                                                                                                                pdo_update('j_hmoney_membercard', array('credit' => $this->_2money(intval($member['credit'] * 100) - intval($gift['credit'] * 100))), array('id' => $memberid));
                                                                                                                            } else {
                                                                                                                                mc_credit_update($uid, 'credit1', -1 * $gift['credit'], array(0 => '', 1 => '收银系统-兑换礼品'));
                                                                                                                            }
                                                                                                                            $this->memberCardInform($memberid);
                                                                                                                            exit(json_encode(array('success' => true, 'out_trade_no' => $outTradeno)));
                                                                                                                        } else {
                                                                                                                            if ($operation == 'sendgift') {
                                                                                                                                $id = $_GPC['id'];
                                                                                                                                $giftrecord = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_giftrecord') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                                                                                                                                if ($giftrecord['status'] == 1) {
                                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '奖品已经发送')));
                                                                                                                                }
                                                                                                                                if ($giftrecord['isget'] == 1) {
                                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '奖品已领取')));
                                                                                                                                }
                                                                                                                                if ($giftrecord['gtype'] == 0) {
                                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '实物无需补发')));
                                                                                                                                }
                                                                                                                                if ($giftrecord['gtype'] == 2) {
                                                                                                                                    if (!$giftrecord['openid']) {
                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '客户没有扫描兑换二维码')));
                                                                                                                                    }
                                                                                                                                    $result = $this->_sendpack($giftrecord['openid'], $giftrecord);
                                                                                                                                    if ($result['errno'] != 0) {
                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => $result['error'])));
                                                                                                                                    }
                                                                                                                                } else {
                                                                                                                                    exit(json_encode(array('success' => true, 'out_trade_no' => $giftrecord['out_trade_no'])));
                                                                                                                                }
                                                                                                                                exit(json_encode(array('success' => true)));
                                                                                                                            } else {
                                                                                                                                if ($operation == 'handover') {
                                                                                                                                    $condition = 'and groupid =\'' . $shopid . '\' and userid=\'' . $userid . '\' and createtime>=\'' . $user['lasttime'] . '\' and createtime<=\'' . TIMESTAMP . '\'';
                                                                                                                                    $items = pdo_fetch('SELECT COUNT(*) as num1,sum(order_fee) as fee1,sum(discount_fee) as fee2,sum(total_fee) as fee3,sum(teafee) as fee4,sum(num) as num2 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1');
                                                                                                                                    $paytypeList1 = pdo_fetchall('SELECT paytype,COUNT(*) as num1,sum(paid_fee1) as fee1 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 group by paytype ');
                                                                                                                                    $paytypeList2 = pdo_fetchall('SELECT paytype2,COUNT(*) as num1,sum(paid_fee2) as fee1 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and paytype2>-1 group by paytype2 ');
                                                                                                                                    $paytypeList = array();
                                                                                                                                    $temp = _getPaytpye('', $shopid);
                                                                                                                                    foreach ($paytypeList1 as $row) {
                                                                                                                                        $paytypeList[$row['paytype']] = array('num' => $row['num1'], 'fee1' => $row['fee1']);
                                                                                                                                    }
                                                                                                                                    foreach ($paytypeList2 as $row) {
                                                                                                                                        if (!isset($paytypeList[$row['paytype2']])) {
                                                                                                                                            $paytypeList[$row['paytype2']] = array('num' => 0, 'fee1' => 0);
                                                                                                                                        }
                                                                                                                                        $paytypeList[$row['paytype2']]['num'] = $paytypeList[$row['paytype2']]['num'] + $row['num1'];
                                                                                                                                        $paytypeList[$row['paytype2']]['fee1'] = $paytypeList[$row['paytype2']]['fee1'] + $row['fee1'];
                                                                                                                                    }
                                                                                                                                    $holdorder = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid =:a  and status<2', array(':a' => $shopid));
                                                                                                                                    $chargeitems = pdo_fetch('SELECT COUNT(*) as num1,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and usetype=0 ');
                                                                                                                                    $chargecount = pdo_fetchall('SELECT paytype,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and usetype=0 and  status=1 group by paytype');
                                                                                                                                    $chargelist = array();
                                                                                                                                    foreach ($chargecount as $row) {
                                                                                                                                        $chargelist[$row['paytype']] = $row['fee1'];
                                                                                                                                    }
                                                                                                                                    $membercount = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and userid=:a and createtime>=\'' . $user['lasttime'] . '\' and createtime<=\'' . TIMESTAMP . '\' ', array(':a' => $userid));
                                                                                                                                    exit(json_encode(array('success' => true, 'item' => $items, 'paytypelist' => $paytypeList, 'holdorder' => $holdorder, 'chargeitems' => $chargeitems, 'chargelist' => $chargelist, 'member' => $membercount, 'time' => date('Y/m/d H:i', $user['lasttime']) . '-' . date('m/d H:i'))));
                                                                                                                                } else {
                                                                                                                                    if ($operation == 'refundgood') {
                                                                                                                                        $gid = $_GPC['id'];
                                                                                                                                        $num = $_GPC['num'];
                                                                                                                                        $result = $this->fun_refunder(2, $gid, $num, $_GPC['fee']);
                                                                                                                                        exit(json_encode(array('success' => $result, 'msg' => '退款失败')));
                                                                                                                                    } else {
                                                                                                                                        if ($operation == 'refundorder') {
                                                                                                                                            $orderid = $_GPC['orderid'];
                                                                                                                                            $remark = $_GPC['remark'];
                                                                                                                                            $paytype = $_GPC['paytype'];
                                                                                                                                            $gid = $_GPC['id'];
                                                                                                                                            $num = $_GPC['num'];
                                                                                                                                            $isgetback = intval($_GPC['isgetback']);
                                                                                                                                            $fee = intval($_GPC['fee'] * 100);
                                                                                                                                            if (!$gid) {
                                                                                                                                                $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $orderid));
                                                                                                                                                if ($order['ispay'] != 1) {
                                                                                                                                                    exit(json_encode(array('success' => true, 'msg' => '订单异常不能退款')));
                                                                                                                                                }
                                                                                                                                                if ($order['paid_fee'] == 0) {
                                                                                                                                                    exit(json_encode(array('success' => true, 'msg' => '订单已退款')));
                                                                                                                                                }
                                                                                                                                                if ($order['paid_fee'] < $fee) {
                                                                                                                                                    exit(json_encode(array('success' => true, 'msg' => '退款金额大于支付金额')));
                                                                                                                                                }
                                                                                                                                                $result = $this->fun_refunder(1, $orderid, $userid, $shopid, $remark, 0, $fee, $paytype, $isgetback);
                                                                                                                                            } else {
                                                                                                                                                $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $gid));
                                                                                                                                                if ($goods['ispay'] != 1) {
                                                                                                                                                    exit(json_encode(array('success' => true, 'msg' => '商品异常不能退款')));
                                                                                                                                                }
                                                                                                                                                if ($goods['total_fee'] == 0) {
                                                                                                                                                    exit(json_encode(array('success' => true, 'msg' => '商品已退款')));
                                                                                                                                                }
                                                                                                                                                $result = $this->fun_refunder(2, $gid, $userid, $shopid, $remark, $num, $fee, $paytype, $isgetback);
                                                                                                                                            }
                                                                                                                                            exit(json_encode($result));
                                                                                                                                        } else {
                                                                                                                                            if ($operation == 'paysuccess') {
                                                                                                                                                $orderid = $_GPC['orderid'];
                                                                                                                                                $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $orderid));
                                                                                                                                                $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE  id=:a', array(':a' => $order['tableid']));
                                                                                                                                                $table2name = pdo_fetchcolumn('SELECT title FROM ' . tablename('j_hmoney_tablecategory') . ' WHERE id=:a', array(':a' => $table['pcate']));
                                                                                                                                                $this->wildDog_socket($shopid);
                                                                                                                                                if (!$order || $order['isconfirm'] || !$order['ispay']) {
                                                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '订单异常')));
                                                                                                                                                }
                                                                                                                                                load()->func('communication');
                                                                                                                                                $temstr = urldecode($cfg['tempmsg_payparama']);
                                                                                                                                                $tempstr = str_replace('|单号|', $order['out_trade_no'], $temstr);
                                                                                                                                                $tempstr = str_replace('|店铺|', $shop['companyname'], $tempstr);
                                                                                                                                                $tempstr = str_replace('|时间|', date('m/d H:i', $order['createtime']), $tempstr);
                                                                                                                                                $tempstr = str_replace('|处理人|', $user['realname'], $tempstr);
                                                                                                                                                $tempstr = str_replace('|总金额|', '￥' . $this->_2money($order['order_fee']), $tempstr);
                                                                                                                                                $tempstr = str_replace('|茶位|', '￥' . $this->_2money($order['teafee']), $tempstr);
                                                                                                                                                $tempstr = str_replace('|优惠|', '￥' . $this->_2money($order['discount_fee']), $tempstr);
                                                                                                                                                $tempstr = str_replace('|实付金额|', '￥' . $this->_2money($order['paid_fee']), $tempstr);
                                                                                                                                                $tempstr = str_replace('|支付方式|', _getPaytpye($order['paytype']), $tempstr);
                                                                                                                                                $tempstr = str_replace('|数量|', $order['num'], $tempstr);
                                                                                                                                                $itemary = json_decode($tempstr, true);
                                                                                                                                                $temp = array();
                                                                                                                                                foreach ($itemary as $key => $val) {
                                                                                                                                                    $temp[$key] = array('value' => $val['value'], 'color' => $val['color'] ? $val['color'] : '#333333');
                                                                                                                                                }
                                                                                                                                                $temp['remark']['value'] = '餐台：' . $table2name . '-' . $table['title'] . "\\n详细收款情况请登录后台查看";
                                                                                                                                                $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account') . ' WHERE uniacid=:uniacid ', array(':uniacid' => $_W['uniacid']));
                                                                                                                                                $acc = WeAccount::create($acid);
                                                                                                                                                if ($user['openid']) {
                                                                                                                                                    $acc->sendTplNotice($user['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                                                                                                }
                                                                                                                                                $userlist = pdo_fetchall('SELECT openid FROM ' . tablename('j_hmoney_user') . ' WHERE  pcate=:a and permission=3 ', array(':a' => $order['groupid']));
                                                                                                                                                if (count($userlist)) {
                                                                                                                                                    foreach ($userlist as $row) {
                                                                                                                                                        if ($row['openid'] != $user['openid']) {
                                                                                                                                                            $data = $acc->sendTplNotice($row['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                                if ($order['openid']) {
                                                                                                                                                    $temp['remark']['value'] = '谢谢惠顾';
                                                                                                                                                    $data = $acc->sendTplNotice($order['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                                                                                                }
                                                                                                                                                if ($order['memberno']) {
                                                                                                                                                    $memberopenid = pdo_fetchcolumn('SELECT openid FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $order['memberno']));
                                                                                                                                                    $temp['remark']['value'] = '谢谢惠顾';
                                                                                                                                                    if ($order['memberno']) {
                                                                                                                                                        $data = $acc->sendTplNotice($order['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                                pdo_update('j_hmoney_order', array('isconfirm' => 1), array('id' => $orderid));
                                                                                                                                                $qrcode = $this->IncreateQrcode($order['out_trade_no'], 2, 1);
                                                                                                                                                pdo_update('j_hmoney_order', array('qrcode' => $qrcode), array('id' => $orderid));
                                                                                                                                                exit(json_encode(array('success' => true)));
                                                                                                                                            } else {
                                                                                                                                                if ($operation == 'getmessage') {
                                                                                                                                                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_msg') . ' WHERE  groupid=:a  and createtime>=:b order by id desc', array(':a' => $shopid, ':b' => strtotime(date('Y-m-d') . ' 00:00:00')));
                                                                                                                                                    $i = 0;
                                                                                                                                                    while ($i < count($list)) {
                                                                                                                                                        $list[$i]['time'] = date('m月d日 H:i', $list[$i]['createtime']);
                                                                                                                                                        $i = $i + 1;
                                                                                                                                                    }
                                                                                                                                                    exit(json_encode(array('success' => true, 'list' => $list)));
                                                                                                                                                } else {
                                                                                                                                                    if ($operation == 'gettoday') {
                                                                                                                                                        $condition = ' and groupid =\'' . $shopid . '\' and createdate=\'' . date('Y-m-d') . '\' ';
                                                                                                                                                        $items = pdo_fetch('SELECT COUNT(*) as num1,sum(order_fee) as fee1,sum(discount_fee) as fee2,sum(paid_fee) as fee3,sum(teafee) as fee4,sum(num) as num2 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and status<4');
                                                                                                                                                        $paytypeList1 = pdo_fetchall('SELECT paytype,COUNT(*) as num1,sum(paid_fee1) as fee1 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and status<4 group by paytype ');
                                                                                                                                                        $paytypeList2 = pdo_fetchall('SELECT paytype2,COUNT(*) as num1,sum(paid_fee2) as fee1 FROM ' . tablename('j_hmoney_order') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and ispay=1 and paytype2>-1 and status<4 group by paytype2 ');
                                                                                                                                                        $paytypeList = array();
                                                                                                                                                        $temp = _getPaytpye('', $shopid);
                                                                                                                                                        foreach ($paytypeList1 as $row) {
                                                                                                                                                            $paytypeList[$row['paytype']] = array('num' => $row['num1'], 'fee1' => $row['fee1']);
                                                                                                                                                        }
                                                                                                                                                        foreach ($paytypeList2 as $row) {
                                                                                                                                                            if (!isset($paytypeList[$row['paytype2']])) {
                                                                                                                                                                $paytypeList[$row['paytype2']] = array('num' => 0, 'fee1' => 0);
                                                                                                                                                            }
                                                                                                                                                            $paytypeList[$row['paytype2']]['num'] = $paytypeList[$row['paytype2']]['num'] + $row['num1'];
                                                                                                                                                            $paytypeList[$row['paytype2']]['fee1'] = $paytypeList[$row['paytype2']]['fee1'] + $row['fee1'];
                                                                                                                                                        }
                                                                                                                                                        $holdorder = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_order') . ' WHERE groupid =\'' . $shopid . '\'  and status=0 ');
                                                                                                                                                        $chargeitems = pdo_fetch('SELECT COUNT(*) as num1,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and usetype=0 ');
                                                                                                                                                        $chargecount = pdo_fetchall('SELECT paytype,sum(cash) as fee1 FROM ' . tablename('j_hmoney_recharge') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and usetype=0 and  status=1 group by paytype');
                                                                                                                                                        $chargelist = array();
                                                                                                                                                        foreach ($chargecount as $row) {
                                                                                                                                                            $chargelist[$row['paytype']] = $row['fee1'];
                                                                                                                                                        }
                                                                                                                                                        $membercount = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_membercard') . ' WHERE groupid =\'' . $shopid . '\' and createtime>=\'' . strtotime(date('Y-m-d 00:00:00')) . '\' ');
                                                                                                                                                        $refundorder = pdo_fetch('SELECT COUNT(*) as num1,sum(num) as num2,sum(fee) as fee1 FROM ' . tablename('j_hmoney_refund') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and rtype=1 ');
                                                                                                                                                        $refundorder['num2'] = $refundorder['num2'] == NULL ? 0 : $refundorder['num2'];
                                                                                                                                                        $refundorder['fee1'] = $refundorder['fee1'] == NULL ? 0 : $refundorder['fee1'];
                                                                                                                                                        $refundgoods = pdo_fetch('SELECT sum(num) as num1,sum(fee) as fee1 FROM ' . tablename('j_hmoney_refund') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and status=1 and rtype=2 ');
                                                                                                                                                        $refundgoods['num1'] = $refundgoods['num1'] == NULL ? 0 : $refundgoods['num1'];
                                                                                                                                                        $refundgoods['fee1'] = $refundgoods['fee1'] == NULL ? 0 : $refundgoods['fee1'];
                                                                                                                                                        $refundary = pdo_fetchall('SELECT paytype,sum(fee) as fee1 FROM ' . tablename('j_hmoney_refund') . ' WHERE weid=\'' . $_W['uniacid'] . '\' ' . $condition . ' and  status=1 group by paytype ');
                                                                                                                                                        $refundlist = array();
                                                                                                                                                        foreach ($refundary as $row) {
                                                                                                                                                            $refundlist[$row['paytype']] = $row['fee1'];
                                                                                                                                                        }
                                                                                                                                                        exit(json_encode(array('success' => true, 'item' => $items, 'paytypelist' => $paytypeList, 'holdorder' => $holdorder, 'chargeitems' => $chargeitems, 'chargelist' => $chargelist, 'member' => $membercount, 'refundorder' => $refundorder, 'refundgoods' => $refundgoods, 'refundlist' => $refundlist, 'time' => date('Y/m/d H:i', $user['lasttime']) . '-' . date('m/d H:i'))));
                                                                                                                                                    } else {
                                                                                                                                                        if ($operation == 'gettodaygoods') {
                                                                                                                                                            $condition = ' and a.groupid =\'' . $shopid . '\' and a.createtime>=\'' . strtotime(date('Y-m-d 00:00:00')) . '\' and a.createtime<=\'' . strtotime(date('Y-m-d 23:59:59')) . '\'';
                                                                                                                                                            $items = pdo_fetchall('SELECT max(a.title) as title2, max(a.goodsn) as sn,sum(a.num) as num1,sum(a.total_fee) as fee1,b.marketprice,AVG(a.price) as price2 FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE a.weid=\'' . $_W['uniacid'] . '\' ' . $condition . '  and a.ispay=1 group by a.goodid order by sum(a.num) desc');
                                                                                                                                                            exit(json_encode(array('success' => true, 'list' => $items)));
                                                                                                                                                        } else {
                                                                                                                                                            if ($operation == 'holdonorder') {
                                                                                                                                                                $tid = $_GPC['tid'];
                                                                                                                                                                $orderid = $_GPC['orderid'];
                                                                                                                                                                $type = $_GPC['holdtype'];
                                                                                                                                                                if ($tid) {
                                                                                                                                                                    $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid=\'' . $_W['uniacid'] . '\'  and id=:a ', array(':a' => $tid));
                                                                                                                                                                    pdo_update('j_hmoney_table', array('pauseorder' => $type, 'endtime' => TIMESTAMP), array('id' => $tid));
                                                                                                                                                                } else {
                                                                                                                                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                                                                                                                    $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid=\'' . $_W['uniacid'] . '\'  and id=:a ', array(':a' => $trade['tableid']));
                                                                                                                                                                    pdo_update('j_hmoney_table', array('pauseorder' => $type, 'endtime' => TIMESTAMP), array('id' => $trade['tableid']));
                                                                                                                                                                }
                                                                                                                                                                exit(json_encode(array('success' => true, 'orderid' => $table['orderid'])));
                                                                                                                                                            } else {
                                                                                                                                                                if ($operation == 'advancepay') {
                                                                                                                                                                    $orderid = intval($_GPC['orderid']);
                                                                                                                                                                    if (!$orderid) {
                                                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '订单号不能为空')));
                                                                                                                                                                    }
                                                                                                                                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                                                                                                                    if ($trade['ispay'] > 0) {
                                                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '已经支付的订单不能再支付')));
                                                                                                                                                                    }
                                                                                                                                                                    $data = array('total_fee' => intval($_GPC['totalfee'] * 100), 'discount' => $_GPC['discount'], 'discount_fee' => intval($_GPC['coupon'] * 100), 'discounttype' => $_GPC['coupontype'], 'memberno' => $_GPC['memberid'], 'remark' => $_GPC['remark'], 'marketing' => $_GPC['marketing'], 'isadvance' => 1, 'teafee' => $trade['teafee'] < intval($_GPC['teafee'] * 100) ? $trade['teafee'] : intval($_GPC['teafee'] * 100));
                                                                                                                                                                    pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                                                                                                                                                    exit(json_encode(array('success' => true)));
                                                                                                                                                                } else {
                                                                                                                                                                    if ($operation == 'fliporder') {
                                                                                                                                                                        if ($user['permission'] < 3) {
                                                                                                                                                                            exit(json_encode(array('success' => false, 'msg' => '必须店长权限方能操作')));
                                                                                                                                                                        }
                                                                                                                                                                        $orderid = $_GPC['orderid'];
                                                                                                                                                                        if (!$orderid) {
                                                                                                                                                                            exit(json_encode(array('success' => false, 'msg' => '请选择订单')));
                                                                                                                                                                        }
                                                                                                                                                                        $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                                                                                                                        if ($trade['ispay'] != 1) {
                                                                                                                                                                            exit(json_encode(array('success' => false, 'msg' => '订单未付款')));
                                                                                                                                                                        }
                                                                                                                                                                        $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid=\'' . $_W['uniacid'] . '\'  and id=:a ', array(':a' => $trade['tableid']));
                                                                                                                                                                        if ($table['status'] != 0 && $table['orderid'] != $orderid) {
                                                                                                                                                                            exit(json_encode(array('success' => false, 'msg' => '【' . $table['title'] . '】目前正在使用中，无法进行反结算。请先将其清台')));
                                                                                                                                                                        }
                                                                                                                                                                        $data = array('paid_fee' => 0, 'paytype' => -1, 'paytype2' => -1, 'marketing' => $_GPC['marketing'], 'paid_fee1' => 0, 'paid_fee2' => 0, 'ispay' => 0, 'pay1_status' => 0, 'pay2_status' => 0, 'time_end' => 0, 'openid' => '', 'cash_fee' => 0, 'transaction_id' => '', 'bank_trade_no' => '', 'status' => 1, 'isconfirm' => 0);
                                                                                                                                                                        pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                                                                                                                                                        pdo_update('j_hmoney_ordergoods', array('ispay' => 0, 'status' => 1), array('orderid' => $orderid));
                                                                                                                                                                        pdo_update('j_hmoney_table', array('status' => 2, 'orderid' => $orderid, 'pauseorder' => 0, 'ispay' => 0, 'newctrl' => 1, 'endtime' => 0), array('id' => $trade['tableid']));
                                                                                                                                                                        $this->wildDog_socket($shopid);
                                                                                                                                                                        exit(json_encode(array('success' => true)));
                                                                                                                                                                    } else {
                                                                                                                                                                        if ($operation == 'chcecknewprint') {
                                                                                                                                                                            $orderid = $_GPC['orderid'];
                                                                                                                                                                            $tid = $_GPC['tid'];
                                                                                                                                                                            if (!$orderid && $tid) {
                                                                                                                                                                                $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid=\'' . $_W['uniacid'] . '\'  and id=:a ', array(':a' => $tid));
                                                                                                                                                                                $orderid = $table['orderid'];
                                                                                                                                                                            }
                                                                                                                                                                            $isnew = pdo_fetchcolumn('SELECT count(*) FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE orderid=:a and isprint=0 ', array(':a' => $orderid));
                                                                                                                                                                            if ($isnew) {
                                                                                                                                                                                exit(json_encode(array('success' => true)));
                                                                                                                                                                            } else {
                                                                                                                                                                                exit(json_encode(array('success' => false)));
                                                                                                                                                                            }
                                                                                                                                                                        } else {
                                                                                                                                                                            if ($operation == 'getordergoods') {
                                                                                                                                                                                $gid = $_GPC['mid'];
                                                                                                                                                                                $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $gid));
                                                                                                                                                                                if ($goods['ispay'] == 1) {
                                                                                                                                                                                    exit(json_encode(array('success' => false, 'msg' => '请使用退款进行退菜')));
                                                                                                                                                                                }
                                                                                                                                                                                exit(json_encode(array('success' => true, 'goods' => $goods)));
                                                                                                                                                                            } else {
                                                                                                                                                                                if ($operation == 'refundgoods') {
                                                                                                                                                                                    $id = intval($_GPC['mid']);
                                                                                                                                                                                    if (!$id) {
                                                                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '请输入需要退单的商品')));
                                                                                                                                                                                    }
                                                                                                                                                                                    if (!$_GPC['num']) {
                                                                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '退菜数量不能为0')));
                                                                                                                                                                                    }
                                                                                                                                                                                    $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE id=:a ', array(':a' => $id));
                                                                                                                                                                                    if ($_GPC['num'] > $goods['num']) {
                                                                                                                                                                                        exit(json_encode(array('success' => false, 'msg' => '退菜数量不能大于下单数量')));
                                                                                                                                                                                    }
                                                                                                                                                                                    $orderid = $goods['orderid'];
                                                                                                                                                                                    $goodid = $goods['goodid'];
                                                                                                                                                                                    $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'userid' => $userid, 'paytype' => -1, 'rtype' => 2, 'num' => $_GPC['num'], 'orderid' => $goods['orderid'], 'gid' => $goods['goodid'], 'goodsn' => $goods['goodsn'], 'title' => $goods['title'], 'price' => $goods['price'], 'ispay' => $goods['ispay'], 'kprinttime' => $goods['kprinttime'], 'reason' => $_GPC['reason'], 'remark' => $_GPC['remark'], 'createtime' => TIMESTAMP, 'createdate' => date('Y-m-d'), 'status' => 1);
                                                                                                                                                                                    pdo_insert('j_hmoney_refund', $data);
                                                                                                                                                                                    $refundorderid = pdo_insertid();
                                                                                                                                                                                    $refund_num = $goods['num'] - $_GPC['num'];
                                                                                                                                                                                    if ($refund_num == 0) {
                                                                                                                                                                                        pdo_delete('j_hmoney_ordergoods', array('id' => $id));
                                                                                                                                                                                    } else {
                                                                                                                                                                                        $data2 = array('num' => $refund_num, 'total_fee' => intval(bcmul($goods['price'], 100) * $refund_num));
                                                                                                                                                                                        pdo_update('j_hmoney_ordergoods', $data2, array('id' => $id));
                                                                                                                                                                                    }
                                                                                                                                                                                    if ($_GPC['getback']) {
                                                                                                                                                                                        $gooditem = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE id=:a ', array(':a' => $goodid));
                                                                                                                                                                                        if ($gooditem['store'] > -1) {
                                                                                                                                                                                            pdo_update('j_hmoney_goods', array('store' => $gooditem['store'] + $_GPC['num']), array('id' => $gooditem['id']));
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                    $gather = pdo_fetch('SELECT sum(num) as num1,count(*) as num2,sum(total_fee) as tfee,sum(oldprice*num*100) as ofee FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and orderid=:a ', array(':a' => $orderid));
                                                                                                                                                                                    $gather2 = pdo_fetch('SELECT sum(a.total_fee) as notfee FROM ' . tablename('j_hmoney_ordergoods') . ' a left join ' . tablename('j_hmoney_goods') . ' b on a.goodid=b.id WHERE b.isnotcoupon=1 and a.orderid=:a ', array(':a' => $orderid));
                                                                                                                                                                                    $data3 = array('order_fee' => $gather['tfee'], 'total_fee' => $gather['tfee'], 'notdiscount_fee' => $gather2['notfee'], 'num' => $gather['num1'], 'species' => $gather['num2'], 'status' => 1);
                                                                                                                                                                                    pdo_update('j_hmoney_order', $data3, array('id' => $orderid));
                                                                                                                                                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                                                                                                                                                                    $table = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_table') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $trade['tableid']));
                                                                                                                                                                                    $goodlist = pdo_fetchall('SELECT a.*,b.memberprice,b.unitname,b.marketprice FROM ' . tablename('j_hmoney_ordergoods') . ' as a LEFT JOIN ' . tablename('j_hmoney_goods') . ' as b ON a.goodid=b.id  WHERE a.orderid=:a order by a.id desc ', array(':a' => $orderid));
                                                                                                                                                                                    exit(json_encode(array('success' => true, 'refundid' => $refundorderid, 'trade' => $trade, 'goods' => $goodlist, 'table' => $table)));
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
if ($_GPC['screen']) {
    include $this->template($cfg['floder'] . '/index1024');
} else {
    include $this->template($cfg['floder'] . '/index');
}